const VALUE = null; // 权重
export const SUCCESS = "success";
export const FAIL = "fail";
export const PAGE_NAME = {
  HOME: "首页",
  GOODS_LIST: "现货大厅",
  SHOP_INDEX: "原料商城",
  SHOP_HOME: "试剂商城",
  POINT_STORE: "积分兑换",
  GOODS_MALL: "现货商城",
  SAMPLE_MALL: "样品商城",
  GOOD_MALL: "有料好店"
};

function handleList(list, idKey = "id", countKey = "count") {
  const resList = [];
  if (list && list.length > 0) {
    list.forEach((e) => {
      resList.push(`${e[idKey]}:${e[countKey]}`);
    });
  }
  return resList;
}

function handleIdList(list, idKey = "id") {
  const resList = [];
  if (list && list.length > 0) {
    list.forEach((e) => {
      resList.push(e[idKey]);
    });
  }
  return resList;
}

function handlegoodsVoList(resList, originList, listKey = "goodsVoList") {
  resList = resList || [];
  if (originList && originList.length > 0) {
    originList.forEach((i) => {
      resList = resList.concat(i[listKey] || []);
    });
  }
  return resList;
}

/**
 * 商品详情页
 * @param {商品ID} goodsId
 * @param {商品名称} goodsName
 */
export function trackEventGoodsDetail(goodsId, goodsName) {
  try {
    getApp().matomo.trackEvent("商品详情页", "商品浏览点击", `${goodsId}:${goodsName}`, VALUE);
  } catch {
    console.log("商品详情页,埋点数据发送失败");
  }
}

/**
 * 会员登录
 * @param {用户ID} userId
 */
export function trackEventBindUserId(userId) {
  try {
    getApp().matomo.setUserId(userId);
  } catch {
    console.log("绑定用户ID,埋点数据发送失败");
  }
}

/**
 * 会员登录
 * @param {用户ID} userId
 */
export function trackEventLogin(userId) {
  try {
    getApp().matomo.trackEvent("会员登录注册", "登录", userId, VALUE);
  } catch {
    console.log("会员登录,埋点数据发送失败");
  }
}

/**
 * 会员注册
 * @param {用户ID} userId
 */
export function trackEventRegist(userId) {
  try {
    getApp().matomo.trackEvent("会员登录注册", "注册", userId, VALUE);
  } catch {
    console.log("会员登录注册,埋点数据发送失败");
  }
}

/**
 * 3.	加入购物车
 * @param {商品ID:商品数量  多个商品请用逗号分隔} skuList
 */
export function trackEventAddCart(skuList = []) {
  try {
    const dataList = handleList(skuList, "goodsId");
    getApp().matomo.trackEvent("电子商务", "加入购物车", dataList.join(), VALUE);
  } catch {
    console.log("加入购物车,埋点数据发送失败");
  }
}

/**
 * 结算
 * @param {商品ID:商品数量  多个商品请用逗号分隔} skuList
 */
export function trackEventSettlement(skuList = []) {
  try {
    const dataList = handleList(skuList, "goodsId", "count");
    getApp().matomo.trackEvent("电子商务", "结算", dataList.join(), VALUE);
  } catch {
    console.log("结算,埋点数据发送失败");
  }
}

/**
 * 提交订单
 * @param {商品ID:商品数量  多个商品请用逗号分隔} skuList
 */
export function trackEventSubmitOrder(skuList = []) {
  try {
    console.log("skuList", skuList);
    const dataList = handleList(skuList, "goodsId", "amount");
    getApp().matomo.trackEvent("电子商务", "提交订单", dataList.join(), VALUE);
  } catch {
    console.log("提交订单,埋点数据发送失败");
  }
}

/**
 * 立即购买
 * @param {商品ID:商品数量  多个商品请用逗号分隔} skuList
 */
export function trackEventBuyNow(skuList = []) {
  try {
    const dataList = handleList(skuList, "goodsId", "amount");
    getApp().matomo.trackEvent("电子商务", "立即购买", dataList.join(), VALUE);
  } catch {
    console.log("立即购买,埋点数据发送失败");
  }
}

/**
 * 7.	企业认证
 * @param {状态} status
 */
export function trackEventCertification(status = "fail") {
  try {
    getApp().matomo.trackEvent("企业认证", "提交", status, VALUE);
  } catch {
    console.log("企业认证,埋点数据发送失败");
  }
}
/**
 *8.	危险化学品资质
 * @param {状态} status
 */
export function trackEventQualification(status = "fail") {
  try {
    getApp().matomo.trackEvent("企业认证", "危险化学品资质提交", status, VALUE);
  } catch {
    console.log("危险化学品资质,埋点数据发送失败");
  }
}

/**
 *9.	易制毒化学品资质
 * @param {状态} status
 */
export function trackEventPrecursorQualification(status = "fail") {
  try {
    getApp().matomo.trackEvent("企业认证", "易制毒化学品资质提交", status, VALUE);
  } catch {
    console.log("易制毒化学品资质,埋点数据发送失败");
  }
}

/**
 *10.	开通电子签约
 * @param {状态} status
 */
export function trackEventElectronicContract(status = "fail") {
  try {
    getApp().matomo.trackEvent("企业认证", "开通电子签约", status, VALUE);
  } catch {
    console.log("开通电子签约,埋点数据发送失败");
  }
}

/**
 *11.	一级导航(曝光)
 * @param {页面名称} pageName
 * @param {商品ID  多个商品请用逗号分隔} dataList
 * @param {原料商城的特殊性，加个标识，无意义} flag
 */
export function trackEventFirstLevel(pageName, goodsList = [], flag) {
  try {
    let dataList = [];
    if (pageName === PAGE_NAME.SHOP_INDEX && !flag) {
      dataList = handlegoodsVoList(dataList, goodsList);
      dataList = handleIdList(dataList, "id");
    } else if (pageName === PAGE_NAME.POINT_STORE) {
      dataList = handleIdList(goodsList, "id");
    } else if (pageName === PAGE_NAME.HOME && flag == 1) {
      dataList = handlegoodsVoList(dataList, goodsList, "goodsEsVoList");
      dataList = handleIdList(dataList, "goodsId");
    } else {
      dataList = handleIdList(goodsList, "goodsId");
    }
    // console.log('商品曝光', pageName, dataList.join());
    if (dataList.length > 0) {
      getApp().matomo.trackEvent("商品曝光", pageName, dataList.join(), VALUE);
    }
  } catch {
    console.log("一级导航(曝光),埋点数据发送失败");
  }
}

/**
 *12.	左侧导航
 * @param {商品类别} category
 * @param {商品ID  多个商品请用逗号分隔} goodsList
 */
export function trackEventLeftLevel(category, goodsList = []) {
  try {
    const dataList = handleIdList(goodsList, "goodsId");
    // console.log('商品曝光', category, dataList);
    getApp().matomo.trackEvent("商品曝光", `商品分类-${category}`, dataList.join(), VALUE);
  } catch {
    console.log("左侧导航,埋点数据发送失败");
  }
}

/**
 *13.	搜索框
 * @param {商品ID  多个商品请用逗号分隔} dataList
 */
export function trackEventSearch(goodsList) {
  try {
    const dataList = handleIdList(goodsList, "goodsId");
    console.log("搜索商品曝光", dataList);
    getApp().matomo.trackEvent("搜索商品曝光", "搜索", dataList, VALUE);
  } catch {
    console.log("搜索框,埋点数据发送失败");
  }
}

/**
 *13.	搜索框关键词
 * @param {关键词} keyword
 */
export function trackEventSearchValue(keyword) {
  try {
    getApp().matomo.trackEvent("搜索", "关键词搜索", keyword, VALUE);
  } catch {
    console.log("搜索框关键词,埋点数据发送失败");
  }
}

/**
 *14.	店铺搜索框关键词
 * @param {关键词} keyword
 * @param {店铺ID} shopId
 */
export function trackEventShopSearchValue(keyword, shopId) {
  try {
    getApp().matomo.trackEvent("店铺搜索", "关键词搜索", `${shopId}:${keyword}`, VALUE);
  } catch {
    console.log("店铺搜索关键词,埋点数据发送失败");
  }
}

/**
 *15.	搜索活动关键字
 * @param {关键词} keyword
 */
export function trackEventActivity() {
  try {
    getApp().matomo.trackEvent("活动", "业绩冲刺", "", VALUE);
  } catch {
    console.log("活动关键词,埋点数据发送失败");
  }
}

/**
 *16.	1月宣传活动按钮点击
 * @param {关键词} keyword
 */
export function trackEventClick() {
  try {
    getApp().matomo.trackEvent("1月宣传活动", "立即采购", "", VALUE);
  } catch {
    console.log("1月宣传活动,立即采购埋点数据发送失败");
  }
}
/**
 *17.	首页-弹窗 点击弹窗
 */
export function trackEventHomeBannerClick() {
  try {
    getApp().matomo.trackEvent("首页-弹窗", "点击弹窗", "", VALUE);
  } catch (e) {
    console.log("首页-弹窗,点击弹窗埋点数据发送失败", e);
  }
}

/**
 *18.	首页-弹窗 点击弹窗
 */
export function trackEventHomeBannerClose() {
  try {
    getApp().matomo.trackEvent("首页-弹窗", "点击关闭弹窗", "", VALUE);
  } catch (e) {
    console.log("首页-弹窗,点击关闭弹窗埋点数据发送失败", e);
  }
}
export function trackEventAsk({ goodsId, name }) {
  try {
    getApp().matomo.trackEvent("咨询", "咨询商家", `${goodsId}:${name}`, VALUE);
  } catch (e) {
    console.log("咨询商家,埋点数据发送失败");
  }
}

export function trackEventClickPhone({ goodsId, name }) {
  try {
    getApp().matomo.trackEvent("咨询商家弹框", "点击手机号", `${goodsId}:${name}`, VALUE);
  } catch (e) {
    console.log("咨询商家弹框-点击手机号,埋点数据发送失败");
  }
}
export function trackEventKnow({ goodsId, name }) {
  try {
    getApp().matomo.trackEvent("咨询商家弹框", "知道了", `${goodsId}:${name}`, VALUE);
  } catch (e) {
    console.log("咨询商家弹框-知道了,埋点数据发送失败");
  }
}
